/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import javax.swing.JOptionPane;

/**
 *
 * @author proyectosbeta
 */
public class VentanaRedimensionarImagen extends javax.swing.JDialog {
    // Variables de clase.
    private boolean redimensionaImagen = false;
    private int anchoImagenRedimensionada;
    private int altoImagenRedimensionada;
    
    /**
     * Creates new form VentanaRedimensionarImagen
     */
    public VentanaRedimensionarImagen(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabelRedimensionarImagen = new javax.swing.JLabel();
        jLabelAnchoImagenRedimensionada = new javax.swing.JLabel();
        jTextFieldAnchoRedimensionarImagen = new javax.swing.JTextField();
        jLabelAltoRedimensionarImagen = new javax.swing.JLabel();
        jTextFieldAltoRedimensionarImagen = new javax.swing.JTextField();
        jButtonAceptar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Redimensionar Imagen");

        jLabelRedimensionarImagen.setText("Redimensionar Imagen");

        jLabelAnchoImagenRedimensionada.setText("Ancho:");

        jTextFieldAnchoRedimensionarImagen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldAnchoRedimensionarImagenActionPerformed(evt);
            }
        });

        jLabelAltoRedimensionarImagen.setText("Alto:");

        jButtonAceptar.setText("Aceptar");
        jButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAceptarActionPerformed(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabelRedimensionarImagen)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelAnchoImagenRedimensionada)
                                    .addComponent(jLabelAltoRedimensionarImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextFieldAnchoRedimensionarImagen, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                                    .addComponent(jTextFieldAltoRedimensionarImagen)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(jButtonAceptar)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonCancelar)))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelRedimensionarImagen)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextFieldAnchoRedimensionarImagen)
                    .addComponent(jLabelAnchoImagenRedimensionada, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextFieldAltoRedimensionarImagen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabelAltoRedimensionarImagen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(3, 3, 3)))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAceptar)
                    .addComponent(jButtonCancelar))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        if(!esValidoNumeroIntroducido(jTextFieldAnchoRedimensionarImagen.getText().toString())){
            jTextFieldAnchoRedimensionarImagen.setText("");
        }else if(!esValidoNumeroIntroducido(jTextFieldAltoRedimensionarImagen.getText().toString())){
             jTextFieldAltoRedimensionarImagen.setText("");
        }
        setVisible(false);
        setRedimensionarImagen(false);
        
        dispose();
        //repaint();
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jTextFieldAnchoRedimensionarImagenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldAnchoRedimensionarImagenActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldAnchoRedimensionarImagenActionPerformed

    private void jButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAceptarActionPerformed
        if(jTextFieldAnchoRedimensionarImagen.getText().toString().equals("")){
            JOptionPane.showMessageDialog(this, "Por favor introduzca un número en "
                + "el ancho.", "Error", JOptionPane.ERROR_MESSAGE);
            
            // Devuelve el foco a jTextAnchoRedimensionarImagen.
            jTextFieldAnchoRedimensionarImagen.requestFocus();
        }else if(jTextFieldAltoRedimensionarImagen.getText().toString().equals("")){
             JOptionPane.showMessageDialog(this, "Por favor introduzca un número en "
                 + "el alto.", "Error", JOptionPane.ERROR_MESSAGE);
           
             // Devuelve el foco a jTextFieldAltoRedimensionarImagen.
             jTextFieldAltoRedimensionarImagen.requestFocus();
        }else if(!esValidoNumeroIntroducido(jTextFieldAnchoRedimensionarImagen.getText().toString())){
             JOptionPane.showMessageDialog(this, "Por favor introduzca correctamente un "
                 + " número al ancho.", "Error", JOptionPane.ERROR_MESSAGE);

             // Setea en blanco jTextAnchoRedimensionarImagen.
             jTextFieldAnchoRedimensionarImagen.setText("");
            
             // Devuelve el foco a jTextAnchoRedimensionarImagen.
             jTextFieldAnchoRedimensionarImagen.requestFocus();
        }else if(!esValidoNumeroIntroducido(jTextFieldAltoRedimensionarImagen.getText().toString())){
             JOptionPane.showMessageDialog(this, "Por favor introduzca correctamente un"
                 + " número al alto.", "Error", JOptionPane.ERROR_MESSAGE);
             
             // Setea en blanco jTextFieldAltoRedimensionarImagen.
             jTextFieldAltoRedimensionarImagen.setText("");
             
             // Devuelve el foco a jTextFieldAltoRedimensionarImagen.
             jTextFieldAltoRedimensionarImagen.requestFocus();
        }else{
             /*
              * Se guarda en las variables de clase anchoImagenRedimensionada y 
              * altoImagenRedimensionada queintrodujo el usuario.
              */
             setAnchoImagenRedimensionada(Integer.parseInt(jTextFieldAnchoRedimensionarImagen.getText().toString()));
             setAltoImagenRedimensionada(Integer.parseInt(jTextFieldAltoRedimensionarImagen.getText().toString()));
             this.setVisible(false);
             setRedimensionarImagen(true);
             repaint();
        }
    }//GEN-LAST:event_jButtonAceptarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VentanaRedimensionarImagen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VentanaRedimensionarImagen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VentanaRedimensionarImagen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VentanaRedimensionarImagen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                VentanaRedimensionarImagen dialog = new VentanaRedimensionarImagen(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    /**
     * Retorna si se puede o no redimensionar la imagen.
     *
     * @return Si se puede o no redimensionar la imagen.
     * @since 1.6
     */
    public boolean isRedimensionarImagen() {
        return this.redimensionaImagen;
    }
    
    /**
     * Establece true si se puede redimensionar imagen; en caso contario, false.
     *
     * @param redimensionarImagen True si se puede redimensionar imagen; en caso contario, false
     * @since 1.6
     */
    public void setRedimensionarImagen(boolean redimensionarImagen) {
        this.redimensionaImagen = redimensionarImagen;
    }
    
    /**
     * Metodo privado que verifica si es valido el numero introducido.
     */
    private boolean esValidoNumeroIntroducido(String numero){
        for(int i = 0; i < numero.length(); i++){
            if(!esNumero(numero.charAt(i))){
                return false;
            }
        } 
        return true;
    }
    
    /**
     * Metodo privado que devuelve true o false si es un numero o no.
     * @param numero
     * @return 
     */
    private boolean esNumero(char numero){
        try{
            // Convertir de char a String.
            String numeroString = String.valueOf(numero);

            Integer.parseInt(numeroString);
            return true;
        }catch(NumberFormatException nfe){            
             return false;
        }
    }

    public int getAnchoImagenRedimensionada() {
        return this.anchoImagenRedimensionada;
    }

    public void setAnchoImagenRedimensionada(int anchoImagenRedimensionada) {
        this.anchoImagenRedimensionada = anchoImagenRedimensionada;
    }

    public int getAltoImagenRedimensionada() {
        return this.altoImagenRedimensionada;
    }

    public void setAltoImagenRedimensionada(int altoImagenRedimensionada) {
        this.altoImagenRedimensionada = altoImagenRedimensionada;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAceptar;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JLabel jLabelAltoRedimensionarImagen;
    private javax.swing.JLabel jLabelAnchoImagenRedimensionada;
    private javax.swing.JLabel jLabelRedimensionarImagen;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldAltoRedimensionarImagen;
    private javax.swing.JTextField jTextFieldAnchoRedimensionarImagen;
    // End of variables declaration//GEN-END:variables
}
